/**
 *
 * @author Ryan Thomas <s548169@sbuniv.edu>
 */
//TODO: Move Socket and related methods to seperate class specifically for comm streams

package guichatclient;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class OptionHandler {
    private String serverIP;
    private int serverPort;
    private Socket connection;
    private String username;
    private boolean autoConnect;
    private boolean firstRun;
    /**
     * 
     */
    
    public OptionHandler(){
        serverIP = "";
        serverPort = 0;
    }
    
    /**
     * 
     */
    
    public void initNetwork(){
        try {
            connection = new Socket(serverIP, serverPort);
        } catch (UnknownHostException ex) {
            Logger.getLogger(OptionHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(OptionHandler.class.getName()).log(Level.SEVERE, null, ex);//TODO: Replace, Multicatch for Exception?
        }
    }
    
    /**
     * Sets IP address of persistent server
     * @param serverIP 
     */
    
    public void setServerIP(String serverIP){
        this.serverIP = serverIP;
    }
    
    /**
     * Sets port for chat communication 
     * @param serverPort 
     */
    
    public void setServerPort(int serverPort){
        this.serverPort = serverPort;
    }
    
    /**
     * 
     * @return 
     */
    
    public String getServerIP(){
        return serverIP;
    }
    
    /**
     * 
     * @return 
     */
    
    public int getServerPort(){
        return serverPort;
    }
    
    /**
     * 
     */
    
    public void refreshSocket(){
        try {
            connection.close();
            connection = new Socket(serverIP, serverPort);
        } catch (IOException ex) {
            Logger.getLogger(OptionHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * 
     * @return 
     */
    
    public Socket getSocket(){
        return connection;
    }
    
    /**
     * 
     * @return 
     */
    
    public DataOutputStream getOutput(){
        DataOutputStream  output = null;
        try {
            output = new DataOutputStream(connection.getOutputStream());
        } catch (IOException ex) {
            Logger.getLogger(OptionHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return output;
    }
    /**
     * 
     * @param username 
     */
    public void setUserName(String username){
        this.username = username;
    }
    /**
     * 
     * @return 
     */
    public String getUserName(){
        return username;
    }
    /**
     * 
     * @param autoConnect 
     */
    public void setAutoConnect(boolean autoConnect){
        this.autoConnect= autoConnect;
    }
    /**
     * 
     * @return 
     */
    public boolean getAutoConnect(){
        return autoConnect;
    }
    
    public void setFirstRun(boolean firstRun){
        this.firstRun = firstRun;
    }
    public Socket getConnection(){
        return connection;
    }
}
